/*
 * Powered By free
 * Web Site: 
 * Since 2018 - 2021
 */

package com.menhai.personmanagement.controller;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.menhai.personmanagement.entity.co.TjSkCo;
import com.menhai.personmanagement.entity.vo.TjInspectphysicalVo;
import com.menhai.personmanagement.utils.ExportSysUserInfoExcel;

import org.apache.commons.codec.binary.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.menhai.personmanagement.entity.co.TjInspectphysicalCo;
import com.menhai.personmanagement.services.TjQueryService;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

@RestController
@RequestMapping("/tjQuery")
public class TjQueryController {
	
	@Autowired
    private TjQueryService tjQueryService;
   
 	@RequestMapping(value = "/getTjRecordListByConditionByPage", method = RequestMethod.POST)
 	public Object getTjRecordListByConditionByPage(HttpServletRequest request, HttpServletResponse response,TjInspectphysicalCo tjInspectphysical) throws Exception {
 		String[] hosnumArray = tjInspectphysical.getHosnumstr().split(",");
 		tjInspectphysical.setHosnumarray(hosnumArray);
 		IPage<List<Map<String,Object>>> list = tjQueryService.getTjRecordListByConditionByPage(tjInspectphysical);
 		return list;
 	}
 	
 	@RequestMapping(value = "/getTjRecordListByCondition", method = RequestMethod.POST)
 	public Object getTjRecordListByCondition(HttpServletRequest request, HttpServletResponse response,TjInspectphysicalCo tjInspectphysical) throws Exception {
 		List<Map<String,Object>> list = tjQueryService.getTjRecordListByCondition(tjInspectphysical);
 		return list;
 	}

  /**
   * @describe 个人体检患者查询
   * @param tjInspectphysicalCo
   * @Return java.lang.Object
   * @Date 2022/2/28 11:39
   * @author shikun
   */
    @RequestMapping(value = "/finishbodycheckup")
    public Object finishbodycheckup(TjInspectphysicalCo tjInspectphysicalCo)  {
        IPage<List<Map<String,Object>>> list = tjQueryService.finishbodycheckup(tjInspectphysicalCo);
        return list;
    }

 	@RequestMapping(value = "/getTjRecordOneById", method = RequestMethod.POST)
 	public Object getTjRecordOneById(HttpServletRequest request, HttpServletResponse response,TjInspectphysicalCo tjInspectphysical) throws Exception {
 		List<Map<String,Object>> list = null;
 		list = tjQueryService.getTjRecordOneById(tjInspectphysical);
 		if(list.size()>0){
 			return list.get(0);
 		}
 		return list;
 	}
 	
 	@RequestMapping("/exportTjRecord")
    public void exportTjRecord(HttpServletResponse response,TjInspectphysicalCo tjInspectphysical){
         try {
        	 if(!org.springframework.util.StringUtils.isEmpty(tjInspectphysical.getHosnumstr())){
        		 String[] hosnumArray = tjInspectphysical.getHosnumstr().split(",");
         		  tjInspectphysical.setHosnumarray(hosnumArray);
        	 }
        	  
              List<Map<String,Object>> list = tjQueryService.getTjRecordListByCondition(tjInspectphysical);

              ExcelWriter writer = ExcelUtil.getWriter();
              writer.addHeaderAlias("name", "姓名");
              writer.addHeaderAlias("sex", "性别");
              writer.addHeaderAlias("age", "年龄");
              writer.addHeaderAlias("chronic", "慢病信息");
              writer.addHeaderAlias("idnumber", "身份证号");
              writer.addHeaderAlias("newaddress", "地址");
              writer.addHeaderAlias("inspectTime", "体检日期");
              writer.addHeaderAlias("state", "状态");
              writer.addHeaderAlias("zongjianjieguo", "体检结果");
              writer.addHeaderAlias("jianyineirong", "建议内容");

              writer.setOnlyAlias(true);
              writer.write(list,true);
              response.setContentType("application/vnd.ms-excel;charset=utf-8");
              String fileName = new String("体检记录信息".getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1);
              response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
              ServletOutputStream out = null;
              try {
                   out = response.getOutputStream();
                   writer.flush(out, true);
              } catch (IOException e) {
                   e.printStackTrace();
              } finally {
                   // 关闭writer，释放内存
                   writer.close();
              }
              //此处记得关闭输出Servlet流
              IoUtil.close(out);

         } catch (Exception e) {
              e.printStackTrace();
         }
    }
 	
 	@RequestMapping(value = "/getTjrcListByConditionByPage", method = RequestMethod.POST)
 	public Object getTjrcListByConditionByPage(HttpServletRequest request, HttpServletResponse response,TjInspectphysicalCo tjInspectphysical) throws Exception {
 		IPage<List<Map<String,Object>>> list = tjQueryService.getTjrcListByConditionByPage(tjInspectphysical);
 		return list;
 	}
 	
 	@RequestMapping(value = "/getCun", method = RequestMethod.POST)
 	public Object getCun(HttpServletRequest request, HttpServletResponse response) throws Exception {
 		String hosnum=request.getParameter("hosnum");
 		List<Map<String,Object>> list = tjQueryService.getCun(hosnum);
 		return list;
 	}
 	

 	
 	@RequestMapping("/exportTjxyysqtj")
    public void exportTjxyysqtj(HttpServletRequest request, HttpServletResponse response){
         try {
        	  String queryDay1=request.getParameter("queryDay1");
        	  String queryDay2=request.getParameter("queryDay2");
        	  String selectType=request.getParameter("selectType");
      		  String hosnum=request.getParameter("hosnum");
              List<Map<String,Object>> list = tjQueryService.getCun(hosnum);

              ExcelWriter writer = ExcelUtil.getWriter();
              List<String> header0 = new LinkedList<>();
              header0.add(queryDay1+"~"+queryDay2+selectType+"统计");
              writer.writeHeadRow(header0);
              writer.merge(0,0,0,11,null,false);     
              List<String> header1 = new LinkedList<>();
              header1.add("村名");
              header1.add("0");
              header1.add("1");
              header1.add("2");
              header1.add("3");
              header1.add("4");
              header1.add("5");
              header1.add("6");
              header1.add("7");
              header1.add("8");
              header1.add("9");
              header1.add("10");
              writer.writeHeadRow(header1);
              List<List<String>> rows = new LinkedList<>();
              list.forEach(val -> {
            	  Map<String,Object> map = val;
                  LinkedList<String> row = CollUtil.newLinkedList(map.get("hosname").toString(),"0","0","0","0","0","0","0","0","0","0","0");
            	  rows.add(row);
              });
              writer.write(rows,true);   
              
              
              response.setContentType("application/vnd.ms-excel;charset=utf-8");
              String fileName = new String("血瘀与湿气统计".getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1);
              response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
              ServletOutputStream out = null;
              try {
                   out = response.getOutputStream();
                   writer.flush(out, true);
              } catch (IOException e) {
                   e.printStackTrace();
              } finally {
                   // 关闭writer，释放内存
                   writer.close();
              }
              //此处记得关闭输出Servlet流
              IoUtil.close(out);

         } catch (Exception e) {
              e.printStackTrace();
         }
    }
 	
 	@RequestMapping(value = "/getQttjzhfxb", method = RequestMethod.POST)
 	public Object getQttjzhfxb(HttpServletRequest request, HttpServletResponse response) throws Exception {
 		String queryDay1=request.getParameter("queryDay1");
 		String queryDay2=request.getParameter("queryDay2");
 		String hosnum=request.getParameter("hosnum");
 		List<Map<String,Object>> list = null;
 		return list;
 	}
 	
 	@RequestMapping("/exportQttjzhfxb")
    public void exportQttjzhfxb(HttpServletRequest request, HttpServletResponse response){
         try {
        	  String queryDay1=request.getParameter("queryDay1");
        	  String queryDay2=request.getParameter("queryDay2");
      		  String hosnum=request.getParameter("hosnum");
              List<Map<String,Object>> list = null;

              ExcelWriter writer = ExcelUtil.getWriter();
              List<String> header0 = new LinkedList<>();
              header0.add(queryDay1+"~"+queryDay2+"体检综合分析表");
              writer.writeHeadRow(header0);
              writer.merge(0,0,0,5,null,false);     
              List<String> header1 = new LinkedList<>();
              header1.add("体检人数");
              header1.add("100人");
              writer.writeRow(header1, true);
              writer.merge(1,1,1,5,null,false); 
              List<String> header2 = new LinkedList<>();
              header2.add("男");
              header2.add("30人");
              header2.add("30%");
              header2.add("女");
              header2.add("70人");
              header2.add("70%");
              writer.writeRow(header2,true);
              List<String> header3 = new LinkedList<>();
              header3.add("年龄段分布");
              header3.add("");
              writer.writeRow(header3,true);
              writer.merge(3,3,1,5,null,false);
              List<String> header4 = new LinkedList<>();
              header4.add("体质辨证");
              header4.add("");
              writer.writeRow(header4,true);
              writer.merge(4,4,1,5,null,false);
              List<String> header5 = new LinkedList<>();
              header5.add("脏腑辨证");
              header5.add("");
              writer.writeRow(header5,true);
              writer.merge(5,5,1,5,null,false);
              List<String> header6 = new LinkedList<>();
              header6.add("血瘀与湿气");
              header6.add("");
              writer.writeRow(header6,true);
              writer.merge(6,6,1,5,null,false);
              List<String> header7 = new LinkedList<>();
              header7.add("疾病风险");
              header7.add("");
              writer.writeRow(header7,true);
              writer.merge(7,7,1,5,null,false);
//              List<List<String>> rows = new LinkedList<>();
//              list.forEach(val -> {
//            	  Map<String,Object> map = val;
//                  LinkedList<String> row = CollUtil.newLinkedList(map.get("hosname").toString(),"0","0","0","0","0","0","0","0","0","0","0");
//            	  rows.add(row);
//              });
//              writer.write(rows,true);   
              
              
              response.setContentType("application/vnd.ms-excel;charset=utf-8");
              String fileName = new String("群体体检综合分析表".getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1);
              response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
              ServletOutputStream out = null;
              try {
                   out = response.getOutputStream();
                   writer.flush(out, true);
              } catch (IOException e) {
                   e.printStackTrace();
              } finally {
                   // 关闭writer，释放内存
                   writer.close();
              }
              //此处记得关闭输出Servlet流
              IoUtil.close(out);

         } catch (Exception e) {
              e.printStackTrace();
         }
    }
 	
 	@GetMapping("/tjjlgecunrenshu")
	public List tjjlgecunrenshu(){
    	return tjQueryService.tjjlgecunrenshu();
	}
	
}